package com.why.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.why.entity.Coursetype;
import com.why.service.CoursetypeService;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (Coursetype)表控制层
 *
 * @author makejava
 * @since 2023-05-20 09:14:05
 */
@RestController
@RequestMapping("/coursetype")
@CrossOrigin
public class CoursetypeController {
    /**
     * 服务对象
     */
    @Autowired
    private CoursetypeService coursetypeService;

    /**
     * 分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public Page<Coursetype> page(int page, int pageSize, String name) {
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Coursetype> queryWrapper = new LambdaQueryWrapper<Coursetype>();
        queryWrapper.like(name != null, Coursetype::getCoursetype, name);
        queryWrapper.orderByDesc(Coursetype::getAddtime);
        coursetypeService.page(pageInfo, queryWrapper);
        return pageInfo;
    }

    @PostMapping("/save")
    public String save(@RequestBody Coursetype coursetype) {
        LambdaQueryWrapper<Coursetype> queryWrapper = new LambdaQueryWrapper<Coursetype>();
        queryWrapper.eq(Coursetype::getCoursetype, coursetype.getCoursetype());
        if (coursetype.getCoursetype() != null && coursetype.getCoursetype() != "") {
            Coursetype coursetype1 = coursetypeService.getOne(queryWrapper);
            if (coursetype1 == null) {
                coursetypeService.save(coursetype);
                return "success";
            } else {
                return "exist";
            }
        } else {
            return "error";
        }
    }

    /**
     * 更新类型
     *
     * @param coursetype
     * @return
     */
    @PostMapping("/update")
    public String update(@RequestBody Coursetype coursetype) {
        //数据合法
        if (coursetype.getCoursetype() != null && coursetype.getCoursetype() != "") {
            //判断需要更新的数据是否存在
            LambdaQueryWrapper<Coursetype> queryWrapper = new LambdaQueryWrapper<Coursetype>();
            queryWrapper.eq(Coursetype::getId, coursetype.getId());
            if (coursetypeService.list(queryWrapper) != null) {
                //判断修改后的名字是否不重复
                LambdaQueryWrapper<Coursetype> queryWrapper2 = new LambdaQueryWrapper<Coursetype>();
                queryWrapper2.eq(Coursetype::getCoursetype, coursetype.getCoursetype());
                Coursetype serviceOne = coursetypeService.getOne(queryWrapper2);
                if (serviceOne == null) {
                    coursetypeService.update(coursetype, queryWrapper);
                    return "success";
                } else {
                    return "exist";
                }
            } else {
                return "error";
            }
        } else {
            return "error";
        }
    }


    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id) {
        LambdaQueryWrapper<Coursetype> queryWrapper = new LambdaQueryWrapper<Coursetype>();
        queryWrapper.eq(Coursetype::getId, id);
        if (coursetypeService.getOne(queryWrapper) != null) {
            coursetypeService.removeById(id);
            return "success";
        } else {
            return "error";
        }
    }

    /**
     * 获取全部信息
     *
     * @return
     */
    @GetMapping("/list")
    public List list() {
        List<Coursetype> list = coursetypeService.list();
        return list;
    }
}

